package com.tdwyz.fictionmanage.controller;

import com.tdwyz.fictionmanage.mapper.UserMapper;
import com.tdwyz.fictionmanage.pojo.Result;
import com.tdwyz.fictionmanage.pojo.User;
import com.tdwyz.fictionmanage.service.UserService;
import com.tdwyz.fictionmanage.utils.Md5Util;
import com.tdwyz.fictionmanage.utils.ThreadLocalUtil;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author 14543
 * @version 1.0
 * @description: TODO
 * @date 2024/7/28 10:28
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Resource
    private UserMapper userMapper;

    @Autowired
    private StringRedisTemplate template;

    /**
     * 用户登录
     * @param username
     * @param password
     * @return
     */
    @PostMapping("login")
    public Result login(@RequestParam String username, @RequestParam String password){
        Result result = userService.login(username, password);
        return result;
    }

    //退出登录
    @PostMapping("logout")
    public Result logout(@RequestHeader("token") String token){
        // 使用ValueOperations来删除Redis中的JWT令牌
        Boolean deleted =template.delete(token);
        if(Boolean.TRUE.equals(deleted)){
            return Result.success("退出登录成功");
        }
        return Result.defeat("退出失败");
    }

    /**
     * 用户注册
     * @param username
     * @param password
     * @return
     */
    @PostMapping("/register")
    public Result register(String username,String password){
        Result result =  userService.register(username,password);
        return result;
    }

    @GetMapping("/getUserInfo")
    public Result getUserInfo(){
        //获取当前登录对象
        Map<String,Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        User user = userMapper.findUserByName(username);
        return Result.success(user);
    }

    /**
     * 修改用户的昵称和邮箱
     * @param user 用户信息
     * @return 影响行数
     */
    @PutMapping("update")
    public Result update(@RequestBody User user){
        int updated = userService.update(user);
        if(updated==1){
            return Result.success("修改成功");
        }
        return Result.defeat("修改成功");
    }

    /**
     * 更新用户头像
     * @param avatarUrl
     * @return
     */
    @PostMapping("updateAvatar")
    public Result updateAvatar(@RequestParam String avatarUrl){
        Map<String,Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        int updatepic = userService.updateAvatar(username,avatarUrl);
        if(updatepic==1){
            return Result.success(null);
        }
        return Result.defeat("更新失败");
    }

    @PostMapping("updatePwd")
    public Result updatePwd(@RequestParam String old_pwd, @RequestParam String new_pwd,
                            @RequestParam String re_pwd,@RequestHeader("token") String token){
        //用户存在进行校验
        //获取当前登录对象
        Map<String,Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");

        User user = userMapper.findUserByName(username);
        if(user==null){
            return Result.defeat("用户不存在");
        }
        //使用原密码与传入的老密码 加密比对
        if(!user.getPassword().equals(Md5Util.getMD5String(old_pwd+user.getSalt()))){
            return Result.defeat("原密码不正确");
        }

        //原密码与新密码不可以一样
        if(old_pwd.equals(re_pwd)){
            return Result.defeat("新密码与原密码一致");
        }
        if(!new_pwd.equals(re_pwd)){
            return Result.defeat("两次密码不一致");
        }

        int updated = userService.updatePwd(new_pwd,username,user.getSalt());
        if(updated==1){
            //清除登录信息
            template.delete(token);
            return Result.success("修改密码成功");
        }
        return Result.defeat("修改密码失败");
    }
}
